// 针对DOM的事件函数
//never返回类型用于那些无法达到终点的函数返回值类型，如果函数抛出一个错误异常、函数包含一个无限循坏
export function addEvent(el: HTMLElement, type: string, fn: () => never, capture = false) {
  //向指定元素添加监听事件
  el.addEventListener(type, fn, !!capture)
}

export function removeEvent(el: HTMLElement, type: string, fn: () => never, capture = false) {
  el.removeEventListener(type, fn, !!capture)
}

//getBoundingClientRect:获得页面中某个元素的左，上，右和下分别相对浏览器视窗的位置

export function getRect(el: HTMLElement) {
  if (el instanceof SVGElement) {
    const rect = el.getBoundingClientRect()
    return {
      top: rect.top,
      left: rect.left,
      width: rect.width,
      height: rect.height
    }
  } else {
    return {
      top: el.offsetTop,
      left: el.offsetLeft,
      width: el.offsetWidth,
      height: el.offsetHeight
    }
  }
}
